package com.apkcore.bl;

/**
 * 给你两个单词 word1 和 word2，请你计算出将 word1 转换成 word2 所使用的最少操作数 。
 * <p>
 * 你可以对一个单词进行如下三种操作：
 * <p>
 * 插入一个字符
 * 删除一个字符
 * 替换一个字符
 *  
 * <p>
 * 示例 1：
 * <p>
 * 输入：word1 = "horse", word2 = "ros"
 * 输出：3
 * 解释：
 * horse -> rorse (将 'h' 替换为 'r')
 * rorse -> rose (删除 'r')
 * rose -> ros (删除 'e')
 * 示例 2：
 * <p>
 * 输入：word1 = "intention", word2 = "execution"
 * 输出：5
 * 解释：
 * intention -> inention (删除 't')
 * inention -> enention (将 'i' 替换为 'e')
 * enention -> exention (将 'n' 替换为 'x')
 * exention -> exection (将 'n' 替换为 'c')
 * exection -> execution (插入 'u')
 *  
 * <p>
 * 提示：
 * <p>
 * 0 <= word1.length, word2.length <= 500
 * word1 和 word2 由小写英文字母组成
 * <p>
 * 链接：https://leetcode-cn.com/problems/edit-distance
 */
public class _72编辑距离 {
    public static void main(String[] args) {
        String str1 = "intention";
        String str2 = "execution";
//        String str1 = "sea";
//        String str2 = "ate";
        System.out.println(new _72编辑距离().minDistance2(str1, str2));
    }

    /**
     * 使用一维数组取代二维数组
     * 只要保存leftTop值就行了，dp[j]= 原有dp[j-1]+1与dp[j]+1与leftTop+1的最小值
     */
    public int minDistance2(String word1, String word2) {
        if (word1 == null || word2 == null) {
            return 0;
        }
        char[] chars1 = word1.toCharArray();
        char[] chars2 = word2.toCharArray();
        if (chars1.length == 0) {
            return chars2.length;
        }
        if (chars2.length == 0) {
            return chars1.length;
        }
        int[] dp = new int[chars2.length + 1];
        for (int j = 1; j <= chars2.length; j++) {
            dp[j] = j;
        }
        for (int i = 1; i <= chars1.length; i++) {
            dp[0] = i;
            int temp = i - 1;
            for (int j = 1; j <= chars2.length; j++) {
                int leftTop = temp;
                temp = dp[j];

                if (chars1[i - 1] == chars2[j - 1]) {
                    dp[j] = leftTop;
                } else {
                    int min = Math.min(dp[j] + 1, dp[j - 1] + 1);
                    min = Math.min(leftTop + 1, min);
                    dp[j] = min;
                }
            }
        }

        return dp[chars2.length];
    }

    public int minDistance(String word1, String word2) {
        if (word1 == null || word2 == null) {
            return 0;
        }
        char[] chars1 = word1.toCharArray();
        char[] chars2 = word2.toCharArray();

        if (chars1.length == 0) {
            return chars2.length;
        }
        if (chars2.length == 0) {
            return chars1.length;
        }

        int[][] dp = new int[chars1.length + 1][chars2.length + 1];
        for (int i = 1; i <= chars1.length; i++) {
            dp[i][0] = i;
        }
        for (int j = 1; j <= chars2.length; j++) {
            dp[0][j] = j;
        }
        for (int i = 1; i <= chars1.length; i++) {
            for (int j = 1; j <= chars2.length; j++) {
                if (chars1[i - 1] == chars2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    int min = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
                    min = Math.min(dp[i - 1][j - 1] + 1, min);
                    dp[i][j] = min;
                }
            }
        }
        for (int i = 0; i <= chars1.length; i++) {
            for (int j = 0; j <= chars2.length; j++) {
                System.out.print(dp[i][j] + ",");
            }
            System.out.println();
        }

        return dp[chars1.length][chars2.length];
    }

}
